# 阶段一：安装依赖（npm 无需虚拟存储或特殊工具）
FROM node:22.14.0-alpine AS deps
WORKDIR /app
# 替换为 npm 的锁定文件 package-lock.json
COPY package.json package-lock.json* ./
# 使用 npm ci 命令（严格依赖 package-lock.json）
RUN --mount=type=cache,target=/root/.npm \
    npm ci --prefer-offline --registry=https://registry.npmmirror.com

# 阶段二：构建应用
FROM node:22.14.0-alpine AS builder
WORKDIR /app
# 直接复制 node_modules（npm 依赖结构为扁平化）
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV CI=true
# 构建时复用缓存
RUN --mount=type=cache,target=/root/.npm \
    npm install --prefer-offline && \
    npm run build

# 阶段三：运行阶段（无需全局安装 npm）
FROM node:22.14.0-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/static ./.next/static

EXPOSE 3000
CMD ["node", "server.js"]